package com.yz.dba.mapper;

import com.yz.common.pojo.Database;
import com.yz.common.pojo.DbLink;
import com.yz.common.pojo.Task;
import com.yz.dba.vo.DatabaseInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author 段成
 * @date 2019/12/5 - 10:22
 */
@Mapper
@Component
public interface DbaMapper {

    @Select("select * from t_dbLink where dstatus=1")
    List<DbLink> getLinkName();

    @Select("select id,databaseName from t_database where linkId=#{linkId} and dstatus=1")
    List<Database> getDatabaseByLinkId(@Param("linkId") Integer linkId);

    @Select("SELECT databaseName FROM t_database WHERE id=#{databaseId} and dstatus=1")
    String getDatabaseNameByDatabaseId(@Param("databaseId") Integer databaseId);

    @Select("SHOW TABLES FROM ${databaseName}")
    List<String> getTablesByDatabaseName(@Param("databaseName") String databaseName);

    @Select("select COLUMN_NAME as fieldNames from information_schema.COLUMNS where table_name = #{tableName} and table_schema =#{databaseName}")
    List<String> getfieldNamesByDatabaseNameAndTableName(@Param("databaseName") String databaseName, @Param("tableName") String tableName);

    @Select("SELECT db.username,db.pass,db.databaseName,dbl.driver,dbl.ip,dbl.`port`,dbl.dbTypeName FROM t_database db INNER JOIN t_dblink dbl ON db.linkId=dbl.id AND db.id = #{databaseId}")
    DatabaseInfo getDatabaseInfo(@Param("databaseId") Integer databaseId);

    @Insert("INSERT INTO t_task (`submitName`, `submitId`, `solveId`, `taskNumber`, `taskType`, `taskTypeName`, `status`, `cdate`, `pdate`, `sourceDatabaseId`, `sourceDatabaseName`, `sourceTableName`, `targetDatabaseId`, `targetDatabaseName`, `targetTableName`, `fieldNames`, `level`, `dstatus`,`coreFields`) VALUES (#{submitName},#{submitId},#{solveId}, #{taskNumber},#{taskType}, #{taskTypeName}, #{status}, #{cdate}, #{pdate}, #{sourceDatabaseId},#{sourceDatabaseName}, #{sourceTableName}, #{targetDatabaseId},#{targetDatabaseName}, #{targetTableName}, #{fieldNames},#{level},#{dstatus},#{coreFields})")
    void commitTask(Task task);

    @Select("select * from t_task where id = #{value} and dstatus=1")
    Task findTaskById(Integer taskId);

    @Select("select id from t_user where username = #{value}")
    Integer findUserIdByUsername(String username);

    @Select("select * from t_task where submitId = #{value} and dstatus=1")
    List<Task> findTask(Integer userId);

    @Select("select taskTypeName from t_tasktype where id = #{value} and dstatus=1")
    String getTaskTypeNameByTypeId(Integer taskType);

    @Update("UPDATE t_task SET dstatus = 0 where id = #{value}")
    int deleteTaskById(Integer taskId);

    @Select("select databaseName from t_database where id=#{value} and dstatus=1")
    String findDatabaseNameById(Integer sourceDatabaseId);

    @Select("select * from t_database where id=#{value} and dstatus=1")
    Database getDatabaseLinkIdById(Integer targetDatabaseId);

    @Select("select * from t_dblink where id=#{value} and dstatus=1")
    DbLink getDbLinkByLinkId(Integer linkId);

    @Update("update t_task set status=#{status} where id = #{id}")
    void updateTaskStatus(@Param("id") Integer id, @Param("status") int status);

    @Select("show full columns from core")
    List<Map> getCoreFields();
}
